package com.evil.application.enums;

import com.evil.common.application.enums.filter.FilterSourceTypeEnum;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

@AllArgsConstructor
public enum ImportTemplateTypeEnum {

    /**
     * 模板类型(1-excel单条导入,2-excel多sheet导入多条数据,3-excel多文件导入,4-xml单条导入)
     */
    EXCEL_SINGLE_ROW(1, "excel单条导入"),
    EXCEL_SHEETS(2, "excel多sheet导入多条数据"),
    EXCEL_FILES(3, "excel多文件导入"),
    XML(4, "xml单条导入"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<ImportTemplateTypeEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ImportTemplateTypeEnum findById(Integer id) {
        return ImportTemplateTypeEnum.isExistById(id).orElseThrow(FilterSourceTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "模板类型参数错误");
    }
}
